/*
 * scroller
 * by  yezhanwang
 * */
(function($,window,undefined){
	$.widget("mobile.scrollview",$.mobile.widget,{
		options: {
		overlayTheme: "a",
		corners: true,
		scrollerView:">.scroller",
		expandDiv:".expand",
		shrinkDiv:".shrink",
		checkDomChange:false,
		initSelector: ":jqmData(role='scrollview')"
	},

	_create: function() {
		var self = this,
			$el = this.element;
		    var id=$el.attr("id");
		    var params={};
		    if($el.hasClass("scroll-Y")){
		    	params={vScrollbar:true};
		    }	    
		    $el.wrapInner("<div class='scroller'></div>");		    
		    self.scroller=new iScroll($el[0],params);
		    var checkDomChanged=this.element.attr("data-dom-changed");
			 if(self.options.checkDomChange||checkDomChanged){
					self._autoRefresh();
			}
		    self._bindPageEvents();
	},
	_bindPageEvents: function() {
		this._thisPage = this.element.closest( ".ui-page" );
		this._on( this._thisPage, {
			"pageshow": "_handlePageShow",
			"pagehide":"_handlePageHide"
		});
	},
	_handlePageShow: function() {
		var self=this;
		self.refresh();
		 var checkDomChanged=this.element.attr("data-dom-changed");
		 if(self.options.checkDomChange||checkDomChanged){
				self._autoRefresh();
		}
	},
	_handlePageHide:function(){
		var self=this;
		clearInterval(self.autoScroller);
	},
	
	_autoRefresh:function(){
		var self=this,
		$el = self.element;
		self.autoScroller=setInterval(function(){
			if (self.scroller.moved || self.scroller.zoomed || self.scroller.animating ||
					(self.scroller.scrollerW == self.scroller.scroller.offsetWidth * self.scroller.scale && self.scroller.scrollerH == self.scroller.scroller.offsetHeight * self.scroller.scale)) return;
			self.scroller.refresh();
		},500);
	},
	refresh:function(delaytime,scrolltoTop){
			var self=this,
			$el = self.element;
			if(scrolltoTop)
				self.scroller.scrollTo(0,0,0);
			self.scroller.refresh(delaytime);
		    	
	}
		
	});
	
	
	$.mobile.document.bind( "pagecreate create", function( e ) {
		$.mobile.scrollview.prototype.enhanceWithin( e.target );
	});

})( jQuery, this );


/**
 * pullToRefresh
 */


(function($,window,undefined){
	
	$.widget("mobile.pullToRefresh",$.mobile.widget,{
		options: {
		overlayTheme: "a",
		corners: true,
		pullUpAction:function(){},
		pullDownAction:function(){},
		pullPosition:"pullUp",
		pullUpLabel:'<div id="pullUp"><div class="message"><div class="pullUpIcon"></div><span class="pullLabel"></span></div></div>',
		pullDownLabel:'<div id="pullDown" ><div class="message"><div class="pullDownIcon"></div><span class="pullLabel"></span></div></div>',
		initSelector: ":jqmData(role='pullToRefresh')"
	},
	_create: function() {
		var self = this,
			$el = this.element;
		    var id=$el.attr("id");
		    if($el.attr("data-position"))
		    	self.options.pullPosition=$el.attr("data-position");
		    if( self.options.pullPosition=="pullDown"){
		    	 self.pullDownEl =$("#pullDown",$el)[0];
		    	 if(!self.pullDownEl)
		    		 $el.prepend(self.options.pullDownLabel);
		    	 self.pullDownOffset = $("#pullDown",$el)[0].offsetHeight;
		    	
		    }
		    if( self.options.pullPosition=="pullUp"){
		    	 self.pullUpEl =$('#pullUp',$el)[0];	
		    	 if(!self.pullUpEl)
		    		 $el.append(self.options.pullUpLabel);
		    	 self.pullUpOffset = $("#pullUp",$el)[0].offsetHeight;
		    }
		    $el.wrapInner("<div class='scroller'></div>");
		    self.scroller = new iScroll($el[0], {
				bounce:true,
				useTransition: true,
				topOffset:  self.pullDownOffset,
				onRefresh: function () {
		    	  	if( self.options.pullPosition=="pullDown"){
				    	 self.pullDownEl =$("#pullDown",$el)[0];
				    	
				    	 self.pullDownOffset = $("#pullDown",$el)[0].offsetHeight;
				    	
				    }
		    	   if( self.options.pullPosition=="pullUp"){
				    	 self.pullUpEl =$('#pullUp',$el)[0];	
				    	 self.pullUpOffset = $("#pullUp",$el)[0].offsetHeight;
				    }
					if ( self.pullDownEl&& self.pullDownEl.className.match('loading')) {
						self.pullDownEl.className = '';
					} else if (self.pullUpEl&&self.pullUpEl.className.match('loading')) {
						 self.pullUpEl.className = '';
					}
				},
				onScrollMove: function () {
					if( self.options.pullPosition=="pullDown"){
				    	 self.pullDownEl =$("#pullDown",$el)[0];
				    	
				    	 self.pullDownOffset = $("#pullDown",$el)[0].offsetHeight;
				    	
				    }
		    	   if( self.options.pullPosition=="pullUp"){
				    	 self.pullUpEl =$('#pullUp',$el)[0];	
				    	 self.pullUpOffset = $("#pullUp",$el)[0].offsetHeight;
				    }
					if (this.y > 5 && self.pullDownEl&& ! self.pullDownEl.className.match('flip')) {
						 self.pullDownEl.className = 'flip';
						this.minScrollY = 0;
					} else if (this.y < 5 && self.pullDownEl&&  self.pullDownEl.className.match('flip')) {
						 self.pullDownEl.className = '';
						this.minScrollY = -self.pullDownOffset;
					} else if (this.y < (this.maxScrollY - 5) &&  self.pullUpEl&&! self.pullUpEl.className.match('flip')) {
						 self.pullUpEl.className = 'flip';
						this.maxScrollY = this.maxScrollY;
					} else if (this.y > (this.maxScrollY + 5) && self.pullUpEl&&  self.pullUpEl.className.match('flip')) {
						 self.pullUpEl.className = '';
						this.maxScrollY = self.pullUpOffset;
					}
				},
				onScrollEnd: function () {
					if( self.options.pullPosition=="pullDown"){
				    	 self.pullDownEl =$("#pullDown",$el)[0];
				    	
				    	 self.pullDownOffset = $("#pullDown",$el)[0].offsetHeight;
				    	
				    }
		    	   if( self.options.pullPosition=="pullUp"){
				    	 self.pullUpEl =$('#pullUp',$el)[0];	
				    	 self.pullUpOffset = $("#pullUp",$el)[0].offsetHeight;
				    }
					if ( self.pullDownEl&& self.pullDownEl.className.match('flip')) {
						 self.pullDownEl.className = 'loading';
						 self.pullDownAction();	
						 self.scroller._checkDOMChanges();
					} else if ( self.pullUpEl&& self.pullUpEl.className.match('flip')) {
						 self.pullUpEl.className = 'loading';
						 self.pullUpAction();	
						 self.scroller._checkDOMChanges();
					}
				}
			});
		    self._bindPageEvents();
		      
	},
	_bindPageEvents: function() {
		this._thisPage = this.element.closest( ".ui-page" );
		this._on( this._thisPage, {
			"pageshow": "_handlePageShow"
		});
	},
	_handlePageShow: function() {
		var self=this;
		self.refresh();
		var checkDomChanged=this.element.attr("data-dom-changed");
		if(self.options.checkDomChange||checkDomChanged){
				self._autoRefresh();
		}
			
	},
	_handlePageHide:function(){
		var self=this;
		clearInterval(self.autoScroller);
	},
	_autoRefresh:function(){
		var self=this,
		$el = self.element;
		self.autoScroller=setInterval(function(){
			if (self.scroller.moved || self.scroller.zoomed || self.scroller.animating ||
					(self.scroller.scrollerW == self.scroller.scroller.offsetWidth * self.scroller.scale && self.scroller.scrollerH == self.scroller.scroller.offsetHeight * self.scroller.scale)) 
				return;
			self.scroller.refresh();
		},500);
	},
	pullDownAction:function(func){
		var self=this,
		$el = this.element;
		var pullDown=$el.attr("pullDownAction");
		if(pullDown)
			eval(pullDown)();
		
	},
	pullUpAction:function(func){
		var self=this,
		$el = this.element;
		var pullUp=$el.attr("pullUpAction");
		if(pullUp)
		    eval(pullUp)();
		
	},
	reloadEvent:function(settings){
		var self=this,
		$el = this.element;
		if(settings.onScrollEnd)
			self.scroller.options.onScrollEnd= function () {
				if( self.options.pullPosition=="pullDown"){
			    	 self.pullDownEl =$("#pullDown",$el)[0];
			    	
			    	 self.pullDownOffset = $("#pullDown",$el)[0].offsetHeight;
			    	
			    }
				if( self.options.pullPosition=="pullUp"){
			    	 self.pullUpEl =$('#pullUp',$el)[0];	
			    	 self.pullUpOffset = $("#pullUp",$el)[0].offsetHeight;
			    }
				if ( self.pullDownEl&& self.pullDownEl.className.match('flip')) {
					 self.pullDownEl.className = 'loading';
					 settings.onScrollEnd();
					 self.scroller._checkDOMChanges();
				} else if ( self.pullUpEl&& self.pullUpEl.className.match('flip')) {
					 self.pullUpEl.className = 'loading';
					 settings.onScrollEnd();
					 self.scroller._checkDOMChanges();
				}
				
			};
		
	},
	refresh:function(delaytime,scrolltoTop){
		var self=this,
		$el = self.element;
		if(scrolltoTop)
			self.scroller.scrollTo(0,0,0);
		self.scroller.refresh(delaytime);
//		console.log("pullToRefresh refresh");
	}
		
	});
	
	$.mobile.document.bind( "pagecreate create", function( e ) {
		$.mobile.pullToRefresh.prototype.enhanceWithin( e.target );
	});

})( jQuery, this );